<?php
if(!checkSession())
{
			function redirect($url)
			{
			?>
				<script type="text/javascript">
					{
					window.location = "<?=$url?>";
					}
				</script>
			<?php 
			}
			redirect("index.php?action=login");
	exit;
}
include('inc/functions_blog.php');
$dateformat = $CONFIG['date_format'];
?>
<script src="./jscript/prettyphoto/js/jquery-1.4.4.min.js" type="text/javascript" charset="utf-8"></script>
<link rel="stylesheet" href="./jscript/prettyphoto/css/prettyPhoto.css" type="text/css" media="screen" charset="utf-8" />
<script src="./jscript/prettyphoto/js/jquery.prettyPhoto.js" type="text/javascript" charset="utf-8"></script>
<div id='postpath'>
		<a title="<?php echo translate('gotofrontpage'); ?>" href="index.php"><?php echo translate('home'); ?></a>
		 &gt; <a href="index.php?action=home&sub=hof" title='<?php echo translate('hof'); ?>'><?php echo translate('hof'); ?></a>
</div>
<br />
<h1><?php echo translate('hof'); ?></h1>
<br />
<?php 	echo translate('hofdescription'); ?>
<br />
<br />
<form method="post" action="index.php?action=home&sub=hof"> 

	<table border="0">
	<tr>
		<td>
			Special:
		</td>
		<td>
			<select name="special-list">
				<option value="">Select one</option>
				<option value="1">Most volume in a day</option>
				<option value="2">Most volume in a week</option>
				<option value="3">Most volume in a month</option>
				<option value="4">Most effective workout</option>
				<option value="5">Most effective user overall</option>
			</select>
		</td>
	</tr>
    <tr>
    <td>
	<b><?php echo ucf(translate('exercise')); ?>:</b>
    </td>
    <td>
	<select name="select_exercise">
    <?php
	$sql= mysql_query("SELECT lifts.exercise_id, lifts.training_id, training.training_id, exercise_name.ex_id, exercise_name.ex_name FROM lifts, training, exercise_name WHERE lifts.training_id=training.training_id AND exercise_name.ex_id=lifts.exercise_id GROUP BY lifts.exercise_id ORDER BY ex_name");
	while($row = mysql_fetch_assoc($sql))
	{	
		$ex_id = $row['exercise_id'];
		$ex_name = getExercisename($row['exercise_id']);	
        echo '<option name="ex_id" value='. $ex_id.'';
		if(isset($_POST['select']) && $_POST['select_exercise']== $ex_id) { echo " selected='selected'"; }
		echo '>'. translate($ex_name) . '</option>';
    }
    ?>
	</select>
    </td>
  
    	<td align="right">
        <b><?php echo ucf(translate('gender')); ?>:</b>
        </td>
        
        <td>
        <select name="gender">
        <option name="gender" value="" <?php if(isset($_POST['select']) && $_POST['gender']== "") { echo " selected='selected'"; } ?>><?php echo ucfirst(translate('both')); ?></option>
		<option name="gender" value="male" <?php if(isset($_POST['select']) && $_POST['gender']== "male") { echo " selected='selected'"; } ?>><?php echo ucf(translate('male')); ?></option>
        <option name="gender" value="female" <?php if(isset($_POST['select']) && $_POST['gender']== "female") { echo " selected='selected'"; } ?>><?php echo ucf(translate('female')); ?></option>		
        </select>    
        </td>
    
 
    
        <td align="right">
        <b><?php echo ucf(translate('reps')); ?>:</b>
        </td>
        
        <td>
        <select name="reps">
        <option name="reps" value="1" <?php if(isset($_POST['select']) && $_POST['reps']== 1) { echo " selected='selected'"; } ?>>1RM</option>
        <option name="reps" value="2" <?php if(isset($_POST['select']) && $_POST['reps']== 2) { echo " selected='selected'"; } ?>>2RM</option>
        <option name="reps" value="3" <?php if(isset($_POST['select']) && $_POST['reps']== 3) { echo " selected='selected'"; } ?>>3RM</option>
        <option name="reps" value="4" <?php if(isset($_POST['select']) && $_POST['reps']== 4) { echo " selected='selected'"; } ?>>4RM</option>
        <option name="reps" value="5" <?php if(isset($_POST['select']) && $_POST['reps']== 5) { echo " selected='selected'"; } ?>>5RM</option>
        <option name="reps" value="6" <?php if(isset($_POST['select']) && $_POST['reps']== 6) { echo " selected='selected'"; } ?>>6RM</option>
        <option name="reps" value="7" <?php if(isset($_POST['select']) && $_POST['reps']== 7) { echo " selected='selected'"; } ?>>7RM</option>
        <option name="reps" value="8" <?php if(isset($_POST['select']) && $_POST['reps']== 8) { echo " selected='selected'"; } ?>>8RM</option>
        <option name="reps" value="9" <?php if(isset($_POST['select']) && $_POST['reps']== 9) { echo " selected='selected'"; } ?>>9RM</option>
        <option name="reps" value="10" <?php if(isset($_POST['select']) && $_POST['reps']== 10) { echo " selected='selected'"; } ?>>10RM</option>
        <option name="reps" value="11" <?php if(isset($_POST['select']) && $_POST['reps']== 11) { echo " selected='selected'"; } ?>>11RM</option>
        <option name="reps" value="12" <?php if(isset($_POST['select']) && $_POST['reps']== 12) { echo " selected='selected'"; } ?>>12RM</option>
        <option name="reps" value="13" <?php if(isset($_POST['select']) && $_POST['reps']== 13) { echo " selected='selected'"; } ?>>13RM</option>
        <option name="reps" value="14" <?php if(isset($_POST['select']) && $_POST['reps']== 14) { echo " selected='selected'"; } ?>>14RM</option>
        <option name="reps" value="15" <?php if(isset($_POST['select']) && $_POST['reps']== 15) { echo " selected='selected'"; } ?>>15RM</option>
        <option name="reps" value="16" <?php if(isset($_POST['select']) && $_POST['reps']== 16) { echo " selected='selected'"; } ?>>16RM</option>
        <option name="reps" value="17" <?php if(isset($_POST['select']) && $_POST['reps']== 17) { echo " selected='selected'"; } ?>>17RM</option>
        <option name="reps" value="18" <?php if(isset($_POST['select']) && $_POST['reps']== 18) { echo " selected='selected'"; } ?>>18RM</option>
        <option name="reps" value="19" <?php if(isset($_POST['select']) && $_POST['reps']== 19) { echo " selected='selected'"; } ?>>19RM</option>
        <option name="reps" value="20" <?php if(isset($_POST['select']) && $_POST['reps']== 20) { echo " selected='selected'"; } ?>>20RM</option>
        <option name="reps" value="0" <?php if(isset($_POST['select']) && $_POST['reps']== 0) { echo " selected='selected'"; } ?>><?php echo ucf(translate('max')); ?></option>
        </select>
        </td>
  
    
        <td align="right">
        <b><?php echo ucf(translate('top')); ?>:</b>
        </td>
        
        <td>
        <select name="nr_to_show">
        <option name="nr_to_show" value="3" <?php if(isset($_POST['select']) && $_POST['nr_to_show']== 3) { echo " selected='selected'"; } ?>>3</option>
        <option name="nr_to_show" value="5" <?php if(isset($_POST['select']) && $_POST['nr_to_show']== 5) { echo " selected='selected'"; } ?>>5</option>
        <option name="nr_to_show" value="10" <?php if(isset($_POST['select']) && $_POST['nr_to_show']== 10) { echo " selected='selected'"; } ?>>10</option>
        <option name="nr_to_show" value="20" <?php if(isset($_POST['select']) && $_POST['nr_to_show']== 20) { echo " selected='selected'"; } ?>>20</option>
        <option name="nr_to_show" value="30" <?php if(isset($_POST['select']) && $_POST['nr_to_show']== 30) { echo " selected='selected'"; } ?>>30</option>
        <option name="nr_to_show" value="40" <?php if(isset($_POST['select']) && $_POST['nr_to_show']== 40) { echo " selected='selected'"; } ?>>40</option>
        <option name="nr_to_show" value="50" <?php if(isset($_POST['select']) && $_POST['nr_to_show']== 50) { echo " selected='selected'"; } ?>>50</option>
        <option name="nr_to_show" value="60" <?php if(isset($_POST['select']) && $_POST['nr_to_show']== 60) { echo " selected='selected'"; } ?>>60</option>
        <option name="nr_to_show" value="70" <?php if(isset($_POST['select']) && $_POST['nr_to_show']== 70) { echo " selected='selected'"; } ?>>70</option>
        <option name="nr_to_show" value="80" <?php if(isset($_POST['select']) && $_POST['nr_to_show']== 80) { echo " selected='selected'"; } ?>>80</option>
        <option name="nr_to_show" value="90" <?php if(isset($_POST['select']) && $_POST['nr_to_show']== 90) { echo " selected='selected'"; } ?>>90</option>
        <option name="nr_to_show" value="100" <?php if(isset($_POST['select']) && $_POST['nr_to_show']== 100) { echo " selected='selected'"; } ?>>100</option>
        </select>
        </td>
    
        <td>
        <input type="submit" style="font-weight:bold;" name="select" value=" <?php echo ucf(translate('show')); ?> "  />
        </td>
    </table>
</form>

<?php
if(isset($_POST['select']) && $_POST['special-list'] =="")
{
	$ex_id = $_POST['select_exercise'];
	$gender = $_POST['gender'];
	$nr_reps = $_POST['reps'];
	$nr_to_show = $_POST['nr_to_show'];

echo "<br /><h1>" . ucf(translate(getExercisename($ex_id))) . " - "; 
if($nr_reps == 0)
{
 echo ucf(translate('max')) . " - " . translate('top') ." " . $nr_to_show ."</h1><br />";
}
else
{
 echo $nr_reps . "RM - " . translate('top') ." " . $nr_to_show ."</h1><br />";
}
	
if($nr_reps == 0)
{
	if($gender == "")
	{
	// Get all the data
	$result = mysql_query("SELECT
    users.user_id
  , users.user_country
  , lifts.kg
  , lifts.url
  , training.place
  , training.date
  , training.training_id
  , training.bodyweight
FROM lifts
INNER JOIN training
ON training.training_id = lifts.training_id 
INNER JOIN users
ON training.user_id = users.user_id 
INNER JOIN
  (SELECT
       training.user_id
     , MAX(lifts.kg) AS maxlift
   FROM lifts
   INNER JOIN training
   ON training.training_id = lifts.training_id 
   WHERE lifts.rep >= '1' 
   AND lifts.exercise_id= ". $ex_id ." 
   GROUP BY user_id
  ) AS ml
ON training.user_id = ml.user_id
AND lifts.kg = ml.maxlift 
WHERE lifts.rep >= '1' 
AND lifts.exercise_id= ". $ex_id ." 
GROUP BY user_id
ORDER BY 
    ABS(lifts.kg) DESC
  , ABS(training.bodyweight) ASC
  , ABS(training.date) ASC
LIMIT ". $nr_to_show ."") or die(mysql_error());
	}
	else
	{
	// Get all the data with sex seleceted
	$result = mysql_query("SELECT
    users.user_id
  ,	users.user_gender
  , users.user_country
  , lifts.kg
  , lifts.url
  , training.place
  , training.date
  , training.training_id
  , training.bodyweight
FROM lifts
INNER JOIN training
ON training.training_id = lifts.training_id 
INNER JOIN users
ON training.user_id = users.user_id 
INNER JOIN
  (SELECT
       training.user_id
     , MAX(lifts.kg) AS maxlift
   FROM lifts
   INNER JOIN training
   ON training.training_id = lifts.training_id 
   WHERE lifts.rep >= '1' 
   AND lifts.exercise_id= ". $ex_id ."
   GROUP BY user_id
  ) AS ml
ON training.user_id = ml.user_id
AND lifts.kg = ml.maxlift 
WHERE lifts.rep >= '1' 
AND lifts.exercise_id= ". $ex_id ."
AND users.user_gender= '". $gender ."'
GROUP BY user_id
ORDER BY 
    ABS(lifts.kg) DESC
  , ABS(training.bodyweight) ASC
  , ABS(training.date) ASC
LIMIT ". $nr_to_show ."") or die(mysql_error());
	}
}
else
{
	if($gender == "")
	{
	// Get all the data
	$result = mysql_query("SELECT
    users.user_id
  , users.user_country
  , lifts.kg
  , lifts.url
  , training.place
  , training.date
  , training.training_id
  , training.bodyweight
FROM lifts
INNER JOIN training
ON training.training_id = lifts.training_id 
INNER JOIN users
ON training.user_id = users.user_id 
INNER JOIN
  (SELECT
       training.user_id
     , MAX(lifts.kg) AS maxlift
   FROM lifts
   INNER JOIN training
   ON training.training_id = lifts.training_id 
   WHERE lifts.rep = ". $nr_reps ." 
   AND lifts.exercise_id= ". $ex_id ." 
   GROUP BY user_id
  ) AS ml
ON training.user_id = ml.user_id
AND lifts.kg = ml.maxlift 
WHERE lifts.rep = ". $nr_reps ." 
AND lifts.exercise_id= ". $ex_id ." 
GROUP BY user_id
ORDER BY 
    ABS(lifts.kg) DESC
  , ABS(training.bodyweight) ASC
  , ABS(training.date) ASC
LIMIT ". $nr_to_show ."") or die(mysql_error());
	}
	else
	{
	// Get all the data with sex seleceted
	$result = mysql_query("SELECT
    users.user_id
  ,	users.user_gender
  , users.user_country
  , lifts.kg
  , lifts.url
  , training.place
  , training.date
  , training.training_id
  , training.bodyweight
FROM lifts
INNER JOIN training
ON training.training_id = lifts.training_id 
INNER JOIN users
ON training.user_id = users.user_id 
INNER JOIN
  (SELECT
       training.user_id
     , MAX(lifts.kg) AS maxlift
   FROM lifts
   INNER JOIN training
   ON training.training_id = lifts.training_id 
   WHERE lifts.rep = ". $nr_reps ." 
   AND lifts.exercise_id= ". $ex_id ."
   GROUP BY user_id
  ) AS ml
ON training.user_id = ml.user_id
AND lifts.kg = ml.maxlift 
WHERE lifts.rep = ". $nr_reps ." 
AND lifts.exercise_id= ". $ex_id ."
AND users.user_gender= '". $gender ."'
GROUP BY user_id
ORDER BY 
    ABS(lifts.kg) DESC
  , ABS(training.bodyweight) ASC
  , ABS(training.date) ASC
LIMIT ". $nr_to_show ."") or die(mysql_error());
	}
}
$num = mysql_num_rows($result);
$i=1;
if($i <= $num)
{ 
		echo "<table border='1' width='100%'>";
		echo "<tr><th>#</th><th>" . ucf(translate('name')) . "</th><th>" . ucf(translate('country')) . "</th><th>" . ucf(translate('place')) . "</th><th>" . translate('date') . "</th><th>" . ucf(translate('bodyweight')) . "</th><th>". translate('lifted') ."</th></tr>";
		
		// keeps getting the next row until there are no more to get
		while($row = mysql_fetch_array( $result )) 
		{
			echo "<tr";  
			if($i == 1): echo " style='background-color:#F9F99F;'"; endif; if($i == 2): echo " style='background-color:#F6F9F9 ;'"; endif; if($i == 3): echo " style='background-color:#EBC79E;'"; endif; echo ">";
			echo "<td align='center'>";
			if($i == 1): echo "<img src='img/gold.png' />"; endif;
			if($i == 2): echo "<img src='img/silver.png' />"; endif; 
			if($i == 3): echo "<img src='img/bronze.png' />"; endif; 
			echo "<b>" . $i++ . "</b>";
			echo "</td>
			
			<td align='center'>";
			echo "<b><a style='color:#555555;' href='index.php?action=profile&id=". $row['user_id'] ."'>" . getNameFromId($row['user_id']) . "</a></b>"; 
			echo "</td>
			
			<td align='center'>";
			echo "<div><img src='img/flags/16/". strtolower($row['user_country']) .".png' /></div> " . translate(ucf(getCountryName(strtolower($row['user_country'])))) . "";
			echo "</td>
			
			<td align='center'>";
			echo $row['place'];
			echo "</td>
			
			<td align='center'>";
			echo formatDate($row['date'], $dateformat);
			echo "</td>
			
			<td align='center'>";
			echo r_nr2(calcFromKg($row['bodyweight'])). $w_metric;
			echo "</td>
			
			<td align='center'>"; 
			echo "<a style='color:#555555;' href='index.php?action=profile&sub=show&tid=". $row['training_id'] ."'>";
			if($w_metric == "kg")
			{
			echo "<b>" .  r_nr2(calcFromKg($row['kg'])). $w_metric . "</a></b>";
				if($row['url'] != "")
				{
					echo " <a href='" . $row['url'] . "' rel='prettyPhoto'><img title='". translate('show_video') ."' src='img/video.png' /></a>";
				}
			}
			else
			{
			echo "<b>" .  r_nr(calcFromKg($row['kg'])). $w_metric . "</a></b>";
				if($row['url'] != "")
				{
					echo " <a href='" . $row['url'] . "' rel='prettyPhoto'><img title='". translate('show_video') ."' src='img/video.png' /></a>";
				}
			}
			echo "</td>
			</tr>"; 
			
		}
		
		echo "</table>
		<div class='fixed'>
							</div>
						</div>
						</div>
					<div class='fixed'>
					</div>
					<div id='footer'>
					<a id='gotop' href='#' onclick='MGJS.goTop();return false;'>" . translate('top') . "</a>
						<div align='center' id='copyright'>&copy; " . translate('copyrightyear') . " " . translate('header_text'). " - " .  translate('curversion') ." - <a href='index.php?action=contact'>" . translate('contact') ."</a> - <a href='index.php?action=about'>" . translate('about') . "</a> - " . translate('onlineusers'). ": " . getActiveUsers() . "<br />
						</div>";
						?>
                        <script type="text/javascript" charset="utf-8">
						  $(document).ready(function(){
							$("a[rel^='prettyPhoto']").prettyPhoto();
						  });
						</script>
                        <?php
			exit;
			
}
else
{
	echo translate('noresultshof');
	echo "<div class='fixed'>
					</div>
				</div>
				</div>
			<div class='fixed'>
			</div>
			<div id='footer'>
			<a id='gotop' href='#' onclick='MGJS.goTop();return false;'>" . translate('top') . "</a>
				<div align='center' id='copyright'>&copy; " . translate('copyrightyear') . " " . translate('header_text'). " - " .  translate('curversion') ." - <a href='index.php?action=contact'>" . translate('contact') ."</a> - <a href='index.php?action=about'>" . translate('about') . "</a> - " . translate('onlineusers'). ": " . getActiveUsers() . "<br />
                </div>";
				?>
                        <script type="text/javascript" charset="utf-8">
						  $(document).ready(function(){
							$("a[rel^='prettyPhoto']").prettyPhoto();
						  });
						</script>
                        <?php
	exit;
} 
}else if(isset($_POST['select']) && $_POST['special-list'] !=""){
	switch($_POST['special-list']){
		case "1":
		
		
			break;
		case "2":
			break;
		case "3":
			break;
		default:
			break;
?>
	
<?php
	}
}
?>